package com.example.wan1.view.system.knowledge.level2;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.wan1.R;
import com.example.wan1.adapter.PerKnowledgeAdapter;
import com.example.wan1.base.fragment.SimpleFragment;
import com.example.wan1.constant.Constants;
import com.example.wan1.model.ArticleBean;
import com.example.wan1.model.common.CommonResList;
import com.example.wan1.utils.AppUtils;
import com.example.wan1.view.collect.HandleCollectTool;
import com.example.wan1.view.login.UserManager;
import com.example.wan1.view.web.JumpTool;
import com.scwang.smart.refresh.header.MaterialHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.List;

import javax.inject.Inject;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import butterknife.BindView;

/***
 * 创建时间：2020/7/9 21:27
 * 创建人：10850
 * 功能描述：PerKnowledge
 */
public class PerKnowledgeFragment extends SimpleFragment implements PerKnowledgeContract.View,
        OnRefreshListener, OnLoadMoreListener {

    private static String PARAM = "PARAM";

    @BindView(R.id.srlheader)
    MaterialHeader mSrlheader;
    @BindView(R.id.knowledge_per_rv)
    RecyclerView mKnowledgePerRv;
    @BindView(R.id.srl)
    SmartRefreshLayout mSrl;
    @Inject
    PerKnowledgeAdapter mAdapter;
    @Inject
    List<ArticleBean> mList;

    private int curPage = 0;
    private int cid;

    @Inject
    PerKnowledgePresenter mPresenter;

    @Override
    protected void initData() {
        mPresenter.getPerKnowledgeData(curPage, cid);
    }

    @Override
    protected void initView() {
        //mSrlheader.setColorSchemeResources(R.color.qc_green);
        mSrl.setOnRefreshListener(this);
        mSrl.setOnLoadMoreListener(this);
        mSrl.setEnableRefresh(false);
        mSrl.setEnableLoadMore(false);
        //mList = new ArrayList<>();
        showLoading();
        //mAdapter = new PerKnowledgeAdapter(R.layout.per_knowledge_rv_item, mList);
        //mAdapter.bindToRecyclerView(mKnowledgePerRv);等同于mKnowledgePerRv.setAdapter(mAdapter);
        mKnowledgePerRv.setAdapter(mAdapter);
        mKnowledgePerRv.setLayoutManager(new LinearLayoutManager(getActivity()));

        //设置点击事件
        mAdapter.setOnItemClickListener((adapter, view, position) -> JumpTool.goWeb(mContext, mList.get(position).getTitle(), mList.get(position).getLink()));

        mAdapter.setOnItemChildClickListener((baseQuickAdapter, view12, i) -> {
            switch (view12.getId()) {
                case R.id.list_collect: {
                    if (!UserManager.getInstance().isLogin()){
//                    if (!AppUtils.isLogin()) {
                        JumpTool.goLogin(getActivity());
                        return;
                    }
                    ArticleBean articleBean = mList.get(i);
                    HandleCollectTool.getInstance().setInfo(articleBean, i, Constants.ARTICLE);
                    ImageView iv = view12.findViewById(R.id.list_collect);
                    if (articleBean.isCollect()) {
                        HandleCollectTool.getInstance().cancelList();
                        iv.setImageResource(R.mipmap.heart_gray);
                    } else {
                        HandleCollectTool.getInstance().collectIn();
                        iv.setImageResource(R.mipmap.heart_red);
                    }
                }
                break;
            }
        });
    }

    @Override
    protected int initLayoutRes() {
        return R.layout.fragment_per_knowledge;
    }

    public PerKnowledgeFragment newInstance(int id) {
        PerKnowledgeFragment baseFragment = new PerKnowledgeFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(PARAM, id);
        baseFragment.setArguments(bundle);
        return baseFragment;
    }

    @Override
    public void onSuccess(CommonResList<ArticleBean> data) {
        mSrl.setEnableRefresh(true);
        mSrl.setEnableLoadMore(true);
        hideLoading();
        //增加获取数据的数量判断
        if (data.getDatas().size() != 0) {
            curPage = data.getCurPage();
            mAdapter.addData(data.getDatas());
            mAdapter.notifyDataSetChanged();
        }
        //增加拉到底的footer，直接使用loadMoreEnd()没有任何效果，考虑可能是SmartRefreshLayout导致的或者是其他的兼容问题
        //https://blog.csdn.net/li_yu_csdn/article/details/80282938 最好写个测试下
        //https://github.com/CymChad/BaseRecyclerViewAdapterHelper/releases?after=alpha-0.2
        if (data.isOver()) {
            mAdapter.loadMoreEnd();
            if (mAdapter.getFooterLayoutCount() == 0) {
                View footer = LayoutInflater.from(mContext).inflate(R.layout.loadmore_view, null);
                mAdapter.addFooterView(footer);
            }
            mSrl.setEnableLoadMore(false);
        }
    }

    @Override
    public void onFailure(String msg) {
        showError();
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        mKnowledgePerRv.postDelayed(() -> {
            mPresenter.getPerKnowledgeData(curPage, cid);
            mSrl.finishLoadMore(true);
        }, 200);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        mList.clear();
        mKnowledgePerRv.postDelayed(() -> {
            mPresenter.getPerKnowledgeData(0, cid);
            mSrl.finishRefresh(true);
        }, 200);
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        cid = getArguments().getInt(PARAM, 0);
    }
}
